package standalone.utils;

import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ui.velocity.VelocityEngineUtils;


public class CreateWrapperConfiguration {
	public static void main(String[] args) {
		try {
			String libDir = args[0];
			String wrapperDir = args[1];

			System.setProperty("user.dir", libDir + "/..");
			ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/utilitiesContext.xml");
			VelocityEngine velocityEngine = (VelocityEngine)context.getBean("velocityEngine");
			Map<String, Object> tmpltData = new HashMap<String, Object>();

			File f = new File(libDir);
			File[] jars = f.listFiles(new FileFilter() {
				@Override
				public boolean accept(File pathname) {
					return pathname.getName().endsWith("jar");
				}
			});
			
			tmpltData.put("libList", jars);
			
			
			FileWriter fw = new FileWriter(wrapperDir + "/wrapper.conf");
			VelocityEngineUtils.mergeTemplate(velocityEngine, "velocity/wrapper.conf.tmplt" , tmpltData, fw);

			fw.flush();
			fw.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
